package village.rainbow.slipin.receiver;

import village.rainbow.slipin.R;
import village.rainbow.slipin.database.dao.NormalDao;
import village.rainbow.slipin.datamodel.DataRow;
import village.rainbow.slipin.datamodel.DataRowBean;
import village.rainbow.slipin.ui.ViewSlipActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmBroadcastReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		
		NormalDao dao = new NormalDao(context);
		
		String tit = intent.getStringExtra("title");
		String msg = intent.getStringExtra("message");
		int notiId = intent.getIntExtra("noti_id", 0);
		try{
			DataRow row = dao.getScene(notiId + "");
			
			// notiId 로 데이터 찾아와서 보내야됨
			Intent i = new Intent();
			i.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
			i.putExtra("DATA", new DataRowBean(row));
			i.setClass(context, ViewSlipActivity.class);
			
			PendingIntent pi = PendingIntent.getActivity( context, 0, i, PendingIntent.FLAG_ONE_SHOT );
			
			long[] vibraPattern = {0, 500, 250, 500 };
			
			Notification.Builder builder = new Notification.Builder(context);
			builder.setSmallIcon(R.drawable.ic_launcher);
			builder.setTicker(tit);
			builder.setContentTitle(tit);
			builder.setContentText(msg);
			builder.setVibrate(vibraPattern);
			builder.setAutoCancel(true);
			builder.setContentIntent(pi);
			
			Notification noti = builder.build();
			NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
			notificationManager.notify( notiId , noti );
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
